//
//  PendApprovalInfoSPInfoMationViewController.m
//  ZJControlsClass
//
//  Created by apple on 2024/11/1.
//

#import "PendApprovalInfoSPInfoMationViewController.h"
#import "PendApprovalInfoSPInfoMationTableViewCell.h"

@interface PendApprovalInfoSPInfoMationViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * datSource;


@end

@implementation PendApprovalInfoSPInfoMationViewController

-(NSMutableArray *)datSource{
    if (!_datSource) {
        _datSource = [NSMutableArray array];
    }
    return _datSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
    
}

-(void)setModel:(ZJPendApprovallInfoModel *)model{
    
//    NSString * str = model.processTemplates.process;
//    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
//    NSMutableArray *tempArray = [NSMutableArray array];
//    
//    // 递归遍历children并添加到数组
//    void (^__block parseChildren)(NSDictionary *) = ^(NSDictionary *node) {
//        if (node[@"children"]) {
//            [tempArray addObject:node];
//            parseChildren(node[@"children"]);
//        } else {
//            [tempArray addObject:node];
//        }
//    };
//    
//    if (dict[@"children"]) {
//        parseChildren(dict[@"children"]);
//    }
//    
//    [self.datSource addObjectsFromArray:tempArray];

  
    
    [self.datSource addObjectsFromArray:model.detailVOList];
    
}



-(void)respondsTo_seeFormBtn:(UIButton *)sender{
    NSInteger idx = sender.tag - 1000;
    
    
    
}




#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PendApprovalInfoSPInfoMationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PendApprovalInfoSPInfoMationTableViewCell"];
    if (!cell) {
        cell = [[PendApprovalInfoSPInfoMationTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"PendApprovalInfoSPInfoMationTableViewCell"];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    id model = self.datSource[indexPath.row];
    
    
    if ([model[@"isFuture"] boolValue]){
        
    }else{
        
        // 枚举值
        NSString * str;
//        NSString * str = model[@"name"];
        if ([model[@"nodeType"] isEqualToString:@"ROOT"]){
            str = @"发起";
        }else if ([model[@"nodeType"] isEqualToString:@"APPROVAL"]){
            str = @"审批";
        }else if ([model[@"nodeType"] isEqualToString:@"FORM"]){
            str = @"表单";
        }else if ([model[@"nodeType"] isEqualToString:@"PAYMENT"]){
            str = @"付款";
        }else if ([model[@"nodeType"] isEqualToString:@"TRAIN"]){
            str = @"培训";
        }else if ([model[@"nodeType"] isEqualToString:@"EXAM"]){
            str = @"考试";
        }else if ([model[@"nodeType"] isEqualToString:@"ACCREDITATION"]){
            str = @"制证";
        }else if ([model[@"nodeType"] isEqualToString:@"SIGN"]){
            str = @"签收";
        }
        cell.typeLabel.text = [NSString stringWithFormat:@"%@",str?str:@""];
        
        NSString * result ;
        
        if (![model[@"result"] isKindOfClass:[NSNull class]]) {
            if ([model[@"result"] isEqualToString:@"complete"]) {
                result = @"已发起";
                cell.typeOneLabel.textColor = COLOR_RGB(50, 113, 237);
            }else if ([model[@"result"] isEqualToString:@"refuse"]){
                result = @"已驳回";
                cell.typeOneLabel.textColor = [UIColor redColor];
            }else if ([model[@"result"] isEqualToString:@"agree"]){
                result = @"同意";
                cell.typeOneLabel.textColor = COLOR_RGB(50, 113, 237);
            }else{
                result = @"未开始";
                cell.typeOneLabel.textColor = [UIColor grayColor];
            }
        }else{
//            result = model[@"result"];
        }
  
        
            
        cell.typeOneLabel.text = result;
        
        
        cell.msgLabel.text = [NSString stringWithFormat:@"%@ %@",self.datSource[indexPath.row][@"user"][@"orgName"]?self.datSource[indexPath.row][@"user"][@"orgName"]:@"",self.datSource[indexPath.row][@"user"][@"name"]?self.datSource[indexPath.row][@"user"][@"name"]:@""];
        cell.timeLabel.text = [NSString stringWithFormat:@"%@",![self.datSource[indexPath.row][@"endTime"] isKindOfClass:[NSNull class]] ? self.datSource[indexPath.row][@"endTime"]:@""];
//        if ([self.datSource[indexPath.row][@"nodeType"] isEqualToString:@"FORM"]){
//            cell.seeBtn.hidden = NO;
//        }else{
            cell.seeBtn.hidden = YES;
//        }
        
        cell.seeBtn.tag = indexPath.row + 1000;
        [cell.seeBtn addTarget:self action:@selector(respondsTo_seeFormBtn:) forControlEvents:(UIControlEventTouchUpInside)];
         
    }
    
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(100);
}



#pragma mark -- 懒加载
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

 

@end
